import {createRouter, createWebHistory} from 'vue-router'
import {useLoginStorage} from "@/stores/loginStorage.js";
import {ElMessage} from "element-plus";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            name: 'root',
            path: '/',
            component: () => import('@/App.vue'),
            redirect: '/index',
        },

        {
            name: 'index',
            path: '/index',
            component: () => import('@/views/IndexView.vue'),
            redirect: '/hub',
            children: [
                {
                    name: 'hub',
                    path: '/hub',
                    component: () => import('@/views/hub/IndexHubView.vue'),
                },

                {
                    name: 'pool',
                    path: '/pool',
                    component: () => import('@/views/pool/IndexPoolView.vue'),
                },

                {
                    name: 'pool-save',
                    path: '/pool/save',
                    component: () => import('@/views/pool/SavePoolView.vue'),
                },

                {
                    name: 'pool-update',
                    path: '/pool/update/:poolId',
                    component: () => import('@/views/pool/UpdatePoolView.vue'),
                },

                {
                    name: 'submit',
                    path: '/submit',
                    component: () => import('@/views/submit/IndexSubmitView.vue')
                },

                {
                    name: 'account',
                    path: '/account',
                    component: () => import('@/views/account/IndexAccountView.vue')
                },

                {
                    name: 'content404',
                    path: '/content404',
                    component: () => import('@/components/NotFoundCpt.vue')
                },
            ],
        },

        {
            name: 'submit-info',
            path: '/submit/info-:submitId',
            component: () => import('@/views/submit/InfoSubmitView.vue'),
        },

        {
            name: 'solving',
            path: '/solving/:poolId',
            component: () => import('@/views/solving/SolvingPoolView.vue'),
        },

        {
            name: 'root404',
            path: '/root404',
            component: () => import('@/components/NotFoundCpt.vue')
        },

        {
            path: '/:pathMatch(.*)*',
            redirect: { name: 'root404' },
        },
    ],
});

router.beforeEach((to, from) => {
    if (to.path === '/index' || to.path === '/hub') return true;

    const loginStore = useLoginStorage();
    if (loginStore.getIsLogin === false) {
        ElMessage.warning('需 登 录 才 能 使 用 该 功 能');
        return {name: 'index'};
    }
});

export default router;
